﻿using AutoMapper;
using Caltech.Models;
using CaltechSoft.Service.EntityFW.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServiceModel = CaltechSoft.Service.DBCommand;

namespace Caltech
{
    public class MappingConfig
    {
        public static void RegisterMappings()
        {
            Mapper.CreateMap<CertificateModel, ServiceModel.CreateNewCertificateCommand>();
            Mapper.CreateMap<TransactionListItemModel, ServiceModel.TransactionListItemModel>();
            Mapper.CreateMap<TransactionCustomerModel, ServiceModel.TransactionCustomerModel>();
            Mapper.CreateMap<StandardModel, ServiceModel.StandardModel>();
            Mapper.CreateMap<StandardModel, ServiceModel.CreateCertificateStandardCommand>();
            Mapper.CreateMap<QuoteModel, Quote>()
                .ForMember(src => src.User, opt => opt.Ignore())
                .ForMember(src => src.Manager, opt => opt.Ignore())
                .ReverseMap();

            Mapper.CreateMap<Quote, QuoteSearchModel>()
                .ForMember(dest => dest.SalesmanName, opt => opt.MapFrom(src => src.Salesman.FullName))
                .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Company.Name))
                .ForMember(dest => dest.GrandTotal, opt => opt.MapFrom(src => CaltotalVND(src)))
                .ForMember(dest => dest.TotalUnit, opt => opt.MapFrom(src => CaltotalQuantity(src)))
                ;

            //Mapper.CreateMap<Po, PoModels>().ReverseMap();
            Mapper.CreateMap<PoModels, Po>()
               .ForMember(src => src.User, opt => opt.Ignore())
               .ForMember(src => src.Manager, opt => opt.Ignore())
               .ReverseMap();


            Mapper.CreateMap<Po, PoSearchModel>()
                .ForMember(dest => dest.SalesmanName, opt => opt.MapFrom(src => src.Salesman.FullName))
                .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Company.Name))
                .ForMember(dest => dest.GrandTotal, opt => opt.MapFrom(src => CaltotalVND(src)))
                .ForMember(dest => dest.TotalUnit, opt => opt.MapFrom(src => CaltotalQuantity(src)))
                ;

            Mapper.CreateMap<Customer, CompanyModel>().ReverseMap();


            Mapper.CreateMap<User, SalesmanModel>()
                .ForMember(dest => dest.Mobile, opt => opt.MapFrom(src => src.PhoneNumber));

            Mapper.CreateMap<User, UserCompactModel>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.FullName));

            Mapper.CreateMap<WorkOrderModel, WorkOrder>()
                .ForMember(src => src.Manager, opt => opt.Ignore())
                .ReverseMap();

            Mapper.CreateMap<QuoteItem, QuoteEquipmentModel>()
                .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom(src => src.Equipment.Manufacturer))
                .ForMember(dest => dest.ModelNumber, opt => opt.MapFrom(src => src.Equipment.ModelNumber))
                .ForMember(dest => dest.PoNumber, opt => opt.MapFrom(src => src.Equipment.PONumber))
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.Equipment.SerialNumber))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Equipment.Name))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Equipment.Description))
                .ReverseMap()
                .ForMember(src => src.Equipment, opt => opt.Ignore());

            Mapper.CreateMap<PoItem, PoEquipmentModel>()
                .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom(src => src.Equipment.Manufacturer))
                .ForMember(dest => dest.ModelNumber, opt => opt.MapFrom(src => src.Equipment.ModelNumber))
                .ForMember(dest => dest.PoNumber, opt => opt.MapFrom(src => src.Equipment.PONumber))
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.Equipment.SerialNumber))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Equipment.Name))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Equipment.Description))
                .ReverseMap()
                .ForMember(src => src.Equipment, opt => opt.Ignore());

            Mapper.CreateMap<WorkOrderDetails, WorkOrderDetailModel>()
                .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom(src => src.Equipment.Manufacturer))
                .ForMember(dest => dest.ModelNumber, opt => opt.MapFrom(src => src.Equipment.ModelNumber))
                .ForMember(dest => dest.PONumber, opt => opt.MapFrom(src => src.Equipment.PONumber))
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.Equipment.SerialNumber))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Equipment.Name))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Equipment.Description))
                .ReverseMap()
                .ForMember(src => src.Equipment, opt => opt.Ignore());

            Mapper.CreateMap<WorkOrderDetailModel, Equipment>()
                .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom(src => src.Manufacturer))
                .ForMember(dest => dest.ModelNumber, opt => opt.MapFrom(src => src.ModelNumber))
                .ForMember(dest => dest.PONumber, opt => opt.MapFrom(src => src.PONumber))
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.SerialNumber))
                ;

            Mapper.CreateMap<User, UserSearchModel>()
                .ForMember(dest => dest.Mobile, opt => opt.MapFrom(src => src.PhoneNumber))
                .ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.UserName))
                .ForMember(dest => dest.Activate, opt => opt.MapFrom(src => !src.LockoutEnabled))
                .ForMember(dest => dest.RoleId, opt => opt.MapFrom(src => src.Roles.FirstOrDefault().RoleId));

            Mapper.CreateMap<Customer, CustomerModel>()
                .ForMember(dest => dest.SalesmanName, opt => opt.MapFrom(src => src.Salesman.FullName))
                .ForMember(dest => dest.IsExistUsername, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.LoginUsername)))
                .ReverseMap();

            Mapper.CreateMap<CustomerModel, Customer>();

            Mapper.CreateMap<Customer, CustomerCompactModel>();


            Mapper.CreateMap<Equipment, EquipmentModel>()
                .ForMember(dest => dest.SearchKeyEquipment, opt => opt.MapFrom(src => src.ModelNumber + " || " + src.Manufacturer))
                .ReverseMap()
                .ForMember(src => src.Salesman, opt => opt.Ignore())
                ;
            Mapper.CreateMap<Equipment, EquipmentSearchModel>();

            Mapper.CreateMap<CustomerEquipment, CustomerEquipmentModel>()
                .ReverseMap()
                .ForMember(src => src.Customer, opt => opt.Ignore())
                .ForMember(src => src.CustomerId, opt => opt.MapFrom(dest => dest.Customer.Id))
                .ForMember(src => src.Salesman, opt => opt.Ignore());

            Mapper.CreateMap<CustomerEquipment, CustomerEquipmentSearchModel>()
               .ForMember(dest => dest.ModelNumber, opt => opt.MapFrom(src => src.Equipment.ModelNumber))
               .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom(src => src.Equipment.Manufacturer))
               .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Equipment.Name))
               .ForMember(dest => dest.SalesmanName, opt => opt.MapFrom(src => src.Salesman.FullName))
               .ForMember(dest => dest.PONumber, opt => opt.MapFrom(src => src.Equipment.PONumber))
               .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Equipment.Description))
               .ReverseMap();

            Mapper.CreateMap<Role, RoleModel>();

            Mapper.CreateMap<AE, AEModel>().ReverseMap();

            Mapper.CreateMap<ProcedureModel, Procedure>()
                .ForMember(src => src.ApproveUser, opt => opt.Ignore())
                .ForMember(src => src.User, opt => opt.Ignore())
                .ReverseMap();

            Mapper.CreateMap<ProcedureItemModel, ProcedureItem>();


            Mapper.CreateMap<ProcedureItem, ProcedureItemModel>()
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.CertificateStandard.Description))
                .ForMember(dest => dest.Manufacturer, opt => opt.MapFrom(src => src.CertificateStandard.Manufacturer))
                .ForMember(dest => dest.ModelNumber, opt => opt.MapFrom(src => src.CertificateStandard.ModelNumber))
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.CertificateStandard.SerialNumber))
                .ForMember(dest => dest.DueDate, opt => opt.MapFrom(src => src.CertificateStandard.DueDate))
                .ForMember(dest => dest.TestReportNumber, opt => opt.MapFrom(src => src.CertificateStandard.TestReportNumber))
                .ForMember(dest => dest.TechnicianName, opt => opt.MapFrom(src => src.CertificateStandard.TechnicianName))
                .ForMember(dest => dest.StandardNo, opt => opt.MapFrom(src => src.CertificateStandard.StandardNo))
                .ForMember(dest => dest.CertificateStandardId, opt => opt.MapFrom(src => src.CertificateStandard.CertificateStandardId))
                ;


            //    public string Manufacturer { get; set; }
            //public string ModelNumber { get; set; }
            //public string SerialNumber { get; set; }
            //public DateTime DueDate { get; set; }
            //public string TestReportNumber { get; set; }
            //public string TechnicianName { get; set; }
            //public string Description { get; set; }

            Mapper.CreateMap<UserHistory, UserHistoryModel>().ReverseMap();
        }


        private static decimal CaltotalVND(Quote data)
        {
            decimal totalVND = 0;
            foreach (var item in data.Items)
            {
                totalVND += Convert.ToDecimal(item.UnitPrice * item.Quantity);
            }
            totalVND = totalVND + data.ServiceCharges;
            decimal totalTax = totalVND * Convert.ToDecimal(data.Vat / 100);

            return totalVND += totalTax;
        }

        private static decimal CaltotalVND(Po data)
        {
            decimal totalVND = 0;
            foreach (var item in data.Items)
            {
                totalVND += Convert.ToDecimal(item.UnitPrice * item.Quantity);
            }
            totalVND = totalVND + data.ServiceCharges;
            decimal totalTax = totalVND * Convert.ToDecimal(data.Vat / 100);

            return totalVND += totalTax;
        }

        private static decimal CaltotalQuantity(Po data)
        {
            int total = 0;
            foreach (var item in data.Items)
            {
                total += item.Quantity;
            }
            return total;
        }

        private static decimal CaltotalQuantity(Quote data)
        {
            int total = 0;
            foreach (var item in data.Items)
            {
                total += item.Quantity;
            }
            return total;
        }
    }
}